package com.dexilaonian.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.adpter.ServiceDetailAdaprer;
import com.dexilaonian.bean.HealthAssBean;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.ImageLoader;
import com.dexilaonian.utils.JsonUtil;
import com.dexilaonian.utils.Okhttp;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class HealthAssActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.base_title_service3)
    TextView baseTitleService3;
    @Bind(R.id.health_tuijian)
    TextView healthTuijian;
    @Bind(R.id.iv_health_tuijian)
    View ivHealthTuijian;
    @Bind(R.id.health_ass)
    TextView healthAss;
    @Bind(R.id.iv_health_ass)
    View ivHealthAss;
    @Bind(R.id.health_heart)
    TextView healthHeart;
    @Bind(R.id.iv_health_heart)
    View ivHealthHeart;
    @Bind(R.id.health_ill)
    TextView healthIll;
    @Bind(R.id.iv_health_ill)
    View ivHealthIll;
    @Bind(R.id.health_ViewPager)
    ViewPager healthViewPager;
    private View mNewsView;
    private ListView listviewNews;
    private View mChronic;
    private ListView listviewChronic;
    private View mRecovery;
    private ListView listviewRecovery;
    private View mFood;
    private ListView listviewFood;
    private List<View> home_list = new ArrayList<>();
    private List<HealthAssBean> list_grid = new ArrayList<>();
    private int home_id;

    @Override
    public int getLayoutID() {
        return R.layout.activity_health_ass;
    }

    @Override
    public void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        baseTitleService3.setText("健康测评");
        init();
        initNews(0);
    }

    private void init() {
        mNewsView = View.inflate(HealthAssActivity.this, R.layout.fragment_new,
                null);
        listviewNews = (ListView) mNewsView.findViewById(R.id.listview_news);

        mChronic = View.inflate(HealthAssActivity.this,
                R.layout.fragment_chronic,
                null);
        listviewChronic = (ListView) mChronic.findViewById(R.id.listview_chronic);

        mRecovery = View.inflate(HealthAssActivity.this,
                R.layout.fragment_reconvery,
                null);
        listviewRecovery = (ListView) mRecovery.findViewById(R.id.listview_recovery);

        mFood = View.inflate(HealthAssActivity.this,
                R.layout.fragment_food,
                null);
        listviewFood = (ListView) mFood.findViewById(R.id.listview_food);

        home_list.clear();
        home_list.add(mNewsView);
        home_list.add(mChronic);
        home_list.add(mRecovery);
        home_list.add(mFood);

        healthTuijian.setOnClickListener(this);
        healthAss.setOnClickListener(this);
        healthHeart.setOnClickListener(this);
        healthIll.setOnClickListener(this);
        healthViewPager.setCurrentItem(0);
        healthViewPager.setOffscreenPageLimit(5);
        ServiceDetailAdaprer adapter = new ServiceDetailAdaprer(HealthAssActivity.this, home_list);
        healthViewPager.setAdapter(adapter);
        healthViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                chanceColorText(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private void chanceColorText(int position) {

        if (position == 0) {
            healthViewPager.setCurrentItem(0);
            healthTuijian.setTextColor(Color.parseColor("#3aafff"));

            healthAss.setTextColor(Color.parseColor("#333333"));
            healthHeart.setTextColor(Color.parseColor("#333333"));
            healthIll.setTextColor(Color.parseColor("#333333"));

            ivHealthTuijian.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
            ivHealthAss.setBackground(null);
            ivHealthHeart.setBackground(null);
            ivHealthIll.setBackground(null);
            initNews(0);

        } else if (position == 1) {
            healthViewPager.setCurrentItem(1);

            healthTuijian.setTextColor(Color.parseColor("#333333"));
            healthAss.setTextColor(Color.parseColor("#3aafff"));
            healthHeart.setTextColor(Color.parseColor("#333333"));
            healthIll.setTextColor(Color.parseColor("#333333"));
            ivHealthTuijian.setBackground(null);
            ivHealthAss.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
            ivHealthHeart.setBackground(null);
            ivHealthIll.setBackground(null);
            initNews(1);
        } else if (position == 2) {
            healthViewPager.setCurrentItem(2);

            healthTuijian.setTextColor(Color.parseColor("#333333"));
            healthAss.setTextColor(Color.parseColor("#333333"));
            healthHeart.setTextColor(Color.parseColor("#3aafff"));
            healthIll.setTextColor(Color.parseColor("#333333"));
            ivHealthTuijian.setBackground(null);
            ivHealthAss.setBackground(null);
            ivHealthHeart.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
            ivHealthIll.setBackground(null);
            initNews(2);
        } else if (position == 3) {
            healthViewPager.setCurrentItem(3);
            healthTuijian.setTextColor(Color.parseColor("#333333"));
            healthAss.setTextColor(Color.parseColor("#333333"));
            healthHeart.setTextColor(Color.parseColor("#333333"));
            healthIll.setTextColor(Color.parseColor("#3aafff"));
            ivHealthTuijian.setBackground(null);
            ivHealthAss.setBackground(null);
            ivHealthHeart.setBackground(null);
            ivHealthIll.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
            initNews(3);
        }
    }

    private void initNews(final int i) {
        String health_url = Constant.base_url + "/health_assessment/list/"+i;
        showProgressBar(true);
        OkHttpUtils.get().url(health_url).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
                showProgressBar(false);
            }

            @Override
            public void onResponse(String response, int id) {
                showProgressBar(false);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 16 || status == 34) {
                        startActivity(new Intent(HealthAssActivity.this, LoginActivity.class));
                        return;
                    }
                    if (status == 0) {
                        String data = jsonObject.optString("data");
                        list_grid.clear();
                        ArrayList<HealthAssBean> homenewsBeen = JsonUtil.json2beans(data, HealthAssBean.class);
                        list_grid.addAll(homenewsBeen);
                        HomeNewsAdapter homeNewsAdapter = new HomeNewsAdapter(HealthAssActivity.this, list_grid);
                        //homeNewsAdapter.clearDeviceList();
                        homeNewsAdapter.setDeviceList((ArrayList<HealthAssBean>) list_grid);
                        if (i == 0) {
                            listviewNews.setAdapter(homeNewsAdapter);
                        } else if (i == 1) {
                            listviewChronic.setAdapter(homeNewsAdapter);
                        } else if (i == 2) {
                            listviewRecovery.setAdapter(homeNewsAdapter);
                        } else if (i == 3) {
                            listviewFood.setAdapter(homeNewsAdapter);
                        }
                        homeNewsAdapter.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @OnClick({R.id.back, R.id.health_tuijian, R.id.health_ass, R.id.health_heart, R.id.health_ill})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.health_tuijian:
                healthViewPager.setCurrentItem(0);
                healthTuijian.setTextColor(Color.parseColor("#3aafff"));
                healthAss.setTextColor(Color.parseColor("#333333"));
                healthHeart.setTextColor(Color.parseColor("#333333"));
                healthIll.setTextColor(Color.parseColor("#333333"));

                ivHealthTuijian.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
                ivHealthAss.setBackground(null);
                ivHealthHeart.setBackground(null);
                ivHealthIll.setBackground(null);
                break;
            case R.id.health_ass:
                healthViewPager.setCurrentItem(1);

                healthTuijian.setTextColor(Color.parseColor("#333333"));
                healthAss.setTextColor(Color.parseColor("#3aafff"));
                healthHeart.setTextColor(Color.parseColor("#333333"));
                healthIll.setTextColor(Color.parseColor("#333333"));
                ivHealthTuijian.setBackground(null);
                ivHealthAss.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
                ivHealthHeart.setBackground(null);
                ivHealthIll.setBackground(null);
                break;
            case R.id.health_heart:
                healthViewPager.setCurrentItem(2);
                healthTuijian.setTextColor(Color.parseColor("#333333"));
                healthAss.setTextColor(Color.parseColor("#333333"));
                healthHeart.setTextColor(Color.parseColor("#3aafff"));
                healthIll.setTextColor(Color.parseColor("#333333"));
                ivHealthTuijian.setBackground(null);
                ivHealthAss.setBackground(null);
                ivHealthHeart.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
                ivHealthIll.setBackground(null);
                break;
            case R.id.health_ill:
                healthViewPager.setCurrentItem(3);
                healthTuijian.setTextColor(Color.parseColor("#333333"));
                healthAss.setTextColor(Color.parseColor("#333333"));
                healthHeart.setTextColor(Color.parseColor("#333333"));
                healthIll.setTextColor(Color.parseColor("#3aafff"));
                ivHealthTuijian.setBackground(null);
                ivHealthAss.setBackground(null);
                ivHealthHeart.setBackground(null);
                ivHealthIll.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
                break;
        }
    }

    class HomeNewsAdapter extends BaseAdapter {

        private FragmentActivity mContext;
        private List<HealthAssBean> mList_grid;

        public HomeNewsAdapter(FragmentActivity activity, List<HealthAssBean> list_grid) {
            mContext = activity;
            mList_grid = list_grid;
        }

        @SuppressWarnings("unchecked")
        public void setDeviceList(ArrayList<HealthAssBean> list_grid) {
            if (mList_grid != null) {
                mList_grid = (ArrayList<HealthAssBean>) list_grid.clone();
                notifyDataSetChanged();
            }
        }

        public void clearDeviceList() {
            if (mList_grid != null) {
                mList_grid.clear();
            }
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mList_grid.size() == 0 ? 0 : mList_grid.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.health_ass, null);
                viewHolder.iv_pic = (ImageView) convertView.findViewById(R.id.iv_health_item);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            String pic = mList_grid.get(position).getPic();
            if (!TextUtils.isEmpty(pic)) {
                ImageLoader.display(HealthAssActivity.this,pic,viewHolder.iv_pic);
            }
            listviewNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int id1 = mList_grid.get(position).getId();
                    Intent intent = new Intent(HealthAssActivity.this, CommunityActivity.class);
                    intent.putExtra("HEALTH", 3);
                    startActivity(intent);
                }
            });
            listviewChronic.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int id2 = mList_grid.get(position).getId();
                    Intent intent = new Intent(HealthAssActivity.this, CommunityActivity.class);
                    intent.putExtra("HEALTH", 4);
                    Log.e("TTTTTHEALTH0", id2 + "");
                    startActivity(intent);
                }
            });
            listviewRecovery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int id3 = mList_grid.get(position).getId();
                    Intent intent = new Intent(HealthAssActivity.this, CommunityActivity.class);
                    intent.putExtra("HEALTH", 5);
                    Log.e("TTTTTHEALTH0", id3 + "");
                    startActivity(intent);
                }
            });
            listviewFood.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int id4 = mList_grid.get(position).getId();
                    Intent intent = new Intent(HealthAssActivity.this, CommunityActivity.class);
                    intent.putExtra("HEALTH", 6);
                    Log.e("TTTTTHEALTH0", id4 + "");
                    startActivity(intent);
                }
            });
            return convertView;
        }

        class ViewHolder {
            ImageView iv_pic;
            TextView tv_textTitle;
            TextView tv_textview;
        }
    }
}
